<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding;

use app\api\controller\Controller;
use app\api\model\ding\ClerkMember as ClerkMemberModel;
use app\api\model\ding\Order as OrderModel;
use app\api\model\ding\OrderGoods as OrderGoodsModel;
use app\api\model\ding\OrderLog as OrderLogModel;
use app\api\model\ding\OrderSeat as OrderSeatModel;
use app\api\model\store\Shop as ShopModel;
use app\api\model\store\shop\Clerk as ClerkModel;
use app\api\service\User as UserService;
use app\common\service\qrcode\Card as CradPoster;
use app\common\service\qrcode\Order as OrderPoster;
use app\common\service\qrcode\PosterNeibu as OrderPosterOut;
use think\facade\Db;
use think\response\Json;

/**
 * 预订管理
 * Class Cart
 * @package app\api\controller
 */
class Index extends Controller
{

    //搜索用户

    public function searchMember($search = "", int $shopId = 0): Json
    {
        $list = [];
        if ($search != '') {

            $list = Db::name('ding_clerk_member')
                ->where('shop_id', $shopId)->group('mobile')->where('real_name', 'like', '%' . $search . '%')->select()->toArray();
        }

        return $this->renderSuccess(['list' => $list], '成功');
    }

    //访问记录-来自个人名片,朋友圈

    public function visitAdd(int $shopId = 0, int $clerkId = 0, int $sourceId = 1): Json
    {

        Db::name('ding_visit_log')
            ->insert(['shop_id' => $shopId, 'clerk_id' => $clerkId, 'source_id' => $sourceId, 'create_time' => time()]);
        return $this->renderSuccess([], '添加成功');
    }

    //睡眠用户

    public function sleepUser(): Json
    {
        $params = $this->request->param();

        $model = new ClerkMemberModel;
        $info  = $model->sleepUser($params);

        return $this->renderSuccess(compact('info'));
    }

    //睡眠用户-短信群发

    public function sleepSend($type = 0): Json
    {

        $model = new ClerkMemberModel;
        $info  = $model->sleepSend($type);

        return $this->renderSuccess(['info' => $info], '已发送成功，可在发送记录查看');
    }

    //排行榜

    public function rankUser(): Json
    {
        $params = $this->request->param();

        $model = new ClerkMemberModel;
        $lists = $model->rankUser($params);

        return $this->renderSuccess(compact('lists'));
    }

    //查询用户
    public function searchUser(): Json
    {
        $params = $this->request->param();

        $model = new ClerkMemberModel;
        $info  = $model->searchUser($params);

        return $this->renderSuccess(compact('info'));
    }
    public function memberList(): Json
    {

        $service = new ClerkMemberModel;
        $list    = $service->getList($this->request->param());

        return $this->renderSuccess(compact('list'));
    }
    //工作动态
    public function listIng(int $shopId = 0): Json
    {

        $service = new OrderModel;
        $list    = $service->getListAll($shopId, $this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    //餐位预订管理
    public function listAll(): Json
    {
        $service = new OrderSeatModel;

        $shopId = $this->request->param()['shopId'];
        $list   = $service->getListAll($shopId, $this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    /*
		*预订历史
	*/

    public function list(): Json
    {

        $shopId  = $this->request->param()['shopId'];
        $service = new OrderModel;
        $list    = $service->getList($shopId, $this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    /*
		 *订单操作记录
	*/
    public function logList(int $orderId, int $type)
    {
        $model = new OrderLogModel;
        $list  = $model->getList($orderId, $type);
        return $this->renderSuccess(compact('list'));
    }
    /*
		 *邀请函访问记录
	*/
    public function visitList(int $shopId)
    {

        $model = new OrderLogModel;
        $list  = $model->getVisitList($shopId);
        return $this->renderSuccess(compact('list'));
    }
    //员工核销订单页面
    public function detail_clerk(int $orderId, int $isUser = 0)
    {

        $detail = OrderModel::getDetail($orderId, $isUser);

        $detail['date_label'] = Date('m月d日', strtotime($detail['date']));

        $shop  = ShopModel::detail($detail['shop_id'], ['logoImage']);
        $user  = UserService::getCurrentLoginUser(true);
        $clerk = Db::name('store_shop_clerk')->where('shop_id', $shop['shop_id'])->where('user_id', $user['user_id'])->where('is_delete', 0)->where('status', 1)->find();
        if (! $clerk) {
            return $this->renderError('非员工，没有权限');
        }

        return $this->renderSuccess(compact('detail'));
    }

    public function detail(int $orderId, int $isUser = 0)
    {
        $detail               = OrderModel::getDetail($orderId, $isUser);
        $detail['date_label'] = Date('m月d日', strtotime($detail['date']));

        $shop = ShopModel::detail($detail['shop_id'], ['logoImage']);
        //点击量
        Db::name('store_shop')->where('shop_id', $detail['shop_id'])->inc('views', 1)->update();
        if ($isUser == 0 && $shop['need_liebian'] == 1) {
            //裂变
            $user = UserService::getCurrentLoginUser(true);
            //是否员工
            $clerk = Db::name('store_shop_clerk')
                ->where('shop_id', $detail['shop_id'])->where('user_id', $user['user_id'])->find();
            if (! $clerk) {

                if ($user['mobile'] != $detail['mobile']) {
                    $log = Db::name('ding_shop_liebian')->where('mobile', $user['mobile'])->where('order_id', $orderId)->find();
                    if ($log) {

                    } else {
                        $liebian['create_time'] = time();
                        $liebian['store_id']    = $detail['store_id'];
                        $liebian['shop_id']     = $detail['shop_id'];
                        $liebian['clerk_id']    = $detail['clerk_id'];
                        $liebian['mobile']      = $user['mobile'];
                        $liebian['order_id']    = $orderId;
                        Db::name('ding_shop_liebian')->insert($liebian);
                        Db::name('store_shop')->where('shop_id', $detail['shop_id'])->inc('liebian_count', 1)->update();
                    }
                }
            }
        }

        //获取分享封面
        //$shop['logo_url'] = 'https://dingxiang.zhanyuankj.cn/0621.jpg';
        $Qrcode    = new OrderPoster($detail, $shop['logo_url']);
        $image_url = $Qrcode->getImage();

        $Qrcode        = new OrderPosterOut($detail);
        $image_url_nei = $Qrcode->getImage();

        $clerkModel = new ClerkModel;
        $clerk      = $clerkModel->detail($detail['clerk_id'], ['user.avatar', 'role']);

        return $this->renderSuccess(compact('detail', 'image_url', 'clerk', 'image_url_nei'));
    }
    public function getOrderPoster(int $orderId, $imgUrl = '')
    {
        $detail               = OrderModel::getDetail($orderId, 0);
        $detail['date_label'] = Date('m月d日', strtotime($detail['date']));

        //获取分享封面

        $Qrcode = new CradPoster($detail, $imgUrl);

        return $this->renderSuccess(compact('detail'));
    }

    public function getOrderGoods(int $orderId)
    {
        $model = new OrderGoodsModel;
        $list  = $model->getList($orderId);

        return $this->renderSuccess(compact('list'));
    }

    //邀请函访问动态

    public function addLog(int $orderId, int $clerkId, int $shopId)
    {
        $data['order_id'] = $orderId;
        $data['clerk_id'] = $clerkId;
        $data['shop_id']  = $shopId;
        $data['type']     = 2;
        OrderLogModel::increased($data);
        return $this->renderSuccess([], '添加成功');
    }

    //预订餐位详情

    public function detail_seat(int $orderId, int $seatId)
    {
        $detail = OrderSeatModel::getDetail(['order_id' => $orderId, 'seat_id' => $seatId]);
        return $this->renderSuccess(compact('detail'));
    }
    /*
		*用户预订
	*/
    public function userAdd(): Json
    {
        $model = new OrderModel;
        $id    = $model->userAdd($this->postForm());
        if ($id) {
            return $this->renderSuccess(['id' => $id], '添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    //落座
    public function add_queu($orderId, $type): Json
    {
        $detail = OrderModel::getDetail($orderId);

        $detail->add_queu($type, $this->postForm());
        return $this->renderSuccess([], '确认成功');
        return $this->renderError($model->getError() ?: '添加失败');
    }

    public function add($type): Json
    {
        $model = new OrderModel;
        $id    = $model->add($type, $this->postForm());
        if ($id) {
            return $this->renderSuccess(['id' => $id], '添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    //用户预约
    public function yuyueAdd(): Json
    {
        $model = new OrderModel;
        $id    = $model->yuyueAdd($this->postForm());
        return $this->renderSuccess([], '您的预订已提交给您的专属服务经理!');
    }
    public function cancel(int $orderId): Json
    {
        $detail = OrderModel::getDetail($orderId);

        if ($detail->cancel()) {
            return $this->renderSuccess([], '取消成功');
        }
        return $this->renderError($model->getError() ?: '取消失败');
    }
    public function queu(int $orderId): Json
    {
        $detail = OrderModel::getDetail($orderId);

        if ($detail->queu()) {
            return $this->renderSuccess([], '确认成功');
        }
        return $this->renderError($model->getError() ?: '确认失败');
    }
    /*
		*调整餐位
	*/
    public function change(int $orderId): Json
    {
        $detail = OrderModel::getDetail($orderId);

        if ($detail->change($this->postForm())) {
            return $this->renderSuccess([], '修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }
    public function edit(int $orderId): Json
    {
        $detail           = OrderModel::getDetail($orderId);
        $data             = $this->postForm();
        $data['order_id'] = $orderId;

        if ($detail->edit($data)) {
            return $this->renderSuccess([], '修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }
}
